package cn.david.user.service.impl;

import cn.david.basic.constant.UserConstants;
import cn.david.basic.exception.BusinessExcepiton;
import cn.david.basic.service.impl.BasicServiceImpl;
import cn.david.basic.util.MD5Utils;
import cn.david.basic.util.StrUtils;
import cn.david.user.domian.Logininfo;
import cn.david.user.domian.User;
import cn.david.user.dto.UserDto;
import cn.david.user.mapper.LogininfoMapper;
import cn.david.user.mapper.UserMapper;
import cn.david.user.service.IUserService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

@Service
public class UserServiceImpl extends BasicServiceImpl<User> implements IUserService {
    @Autowired
    private LogininfoMapper logininfoMapper;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private RedisTemplate redisTemplate;
    @Override
    public void phoneRegister(UserDto userDto) {
        //1.校验数据
        String phone = userDto.getPhone();
        //1.1 参数不能为空
        if (StringUtils.isEmpty(phone)
                || StringUtils.isEmpty(userDto.getPasswordRepeat())
                || StringUtils.isEmpty(userDto.getPassword())
                || StringUtils.isEmpty(userDto.getCode())){
            throw new BusinessExcepiton("参数不能为空");
        }
        //1.2 手机不能被注册  查询user
        User userTmp = userMapper.loadByphone(phone);
        if (userTmp!=null){
            throw new BusinessExcepiton("手机号码已注册");
        }
        //1.3 两次密码是否一致
        if (!userDto.getPassword().equals(userDto.getPasswordRepeat())){
            throw new BusinessExcepiton("两次密码不一致，请重新输入");
        }

        //2 验证码校验  不要区分大小写
        Object o = redisTemplate.opsForValue().get(UserConstants.USER_REGISTER_CODE_PREFIX + phone);
        if (o==null){
            throw new BusinessExcepiton("验证码已失效");
        }
        String code =((String)o).split(":")[0];
        if (!code.equalsIgnoreCase(userDto.getCode())){
            throw new BusinessExcepiton("验证码错误");
        }
        //3 注册
        User user = UserDto2User(userDto);
        Logininfo logininfo = user2LoginInfo(user);
        logininfoMapper.save(logininfo);
        user.setLogininfo_id(logininfo.getId());
        userMapper.save(user);
    }

    private User UserDto2User(UserDto userDto) {
        User user = new User();
        user.setUsername(userDto.getPhone());
        user.setPhone(userDto.getPhone());
        //生成随机32位的salt
        String salt = StrUtils.getComplexRandomString(32);
        String md5Pwd = MD5Utils.encrypByMd5(userDto.getPassword() + salt);
        user.setSalt(salt);
        user.setPassword(md5Pwd);
        return user;
    }

    private Logininfo user2LoginInfo(User user) {
        Logininfo logininfo = new Logininfo();
        BeanUtils.copyProperties(user, logininfo);
        logininfo.setType(1);
        return logininfo;
        //3.1 登录信息 和 用户
    }
}
